package com.example.demo;

import com.example.demo.converter.BytesToMoneyConverter;
import com.example.demo.converter.MoneyToBytesConverter;
import com.example.demo.model.Emp;
import com.example.demo.reposition.EmpReposition;
import com.example.demo.service.EmpService;
import io.lettuce.core.ReadFrom;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.convert.RedisCustomConversions;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Arrays;
import java.util.Map;

@SpringBootApplication
@Slf4j
@EnableJpaRepositories
@EnableTransactionManagement
public class DemoApplication implements CommandLineRunner {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Autowired
	EmpReposition empReposition;

	@Autowired
	EmpService empService;

	@Bean
	public RedisTemplate<String, Emp> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<String, Emp> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	public LettuceClientConfigurationBuilderCustomizer customizer() {
		return builder -> builder.readFrom(ReadFrom.MASTER_PREFERRED);
	}
	@Bean
	public RedisCustomConversions redisCustomConversions() {
		return new RedisCustomConversions(
				Arrays.asList(new MoneyToBytesConverter(), new BytesToMoneyConverter()));
	}


	@Override
	public void run(String... args) throws Exception {
		log.info("out");
		Emp emp = Emp.builder().name("张三").job("主管")
				.salary(Money.of(CurrencyUnit.of("CNY"), 20.0)).build();

		Emp e = empService.findOneFromCache("Tom").get();
		log.info("return {}",e);

	}



}
